home *** CD-ROM | disk | FTP | other *** search
- Path: noc.tor.hookup.net!the-fix.sos.on.ca
- From: <verneb@the-fix.sos.on.ca>
- Date: Sat,20 Jan 96 14:37:10 -0500
- Newsgroups: comp.lang.c
- Subject: Re: Help: floating point/use of floor
- Message-ID: <tcpnntpd.16.1.20.14.37.10.2781597121.351170@the-fix.sos.on.ca>
- References: <alun.champion.96jan19120052@g7240065.bridge.bst.bls.com>
- Organization: the FIX - Kitchener, Ontario CANADA
- NNTP-Posting-Host: the-fix.sos.on.ca
- X-MajorTCP-Version: MajorTCP/IP [1.81-2]
-
- To: INT:Alun.Champion@bridge.
-
- Int:alun.champion@bridge.,
-
- I>In article <4dogri$fta@gate.service.britgas.co.uk>
- I>andy.crutchlow@service.britgas.co.uk (Andy Crutchlow) writes:
- I>
- I>: Can anyone help.
- I>
- I>: Make the following display 75.99
- I>: main()
- I>: {
- I>: int i;
- I>: float f;
- I>: i = 7599;
- I>: f = i / 100.00;
- I>: printf("%f\n",f);/* change to: printf("%.2f\n",f);
- I>: return 0; /* don't forget the return here */
- }
- I>
-
- problem with floats (and doubles) is that some numbers are not
- I>directly representable.
- I>What may not be directly representable in floats may be representable in
- I>doubles.
- I>I can make it print 75.99 but I can't guarentee yours will do the same.
- I>
- I>..h>
- I>
- I>int
- I>main(void)
- I>{
- I>float f = 75.99;
- I>double d = 75.99;
- I>printf("%f\n", f); /* prints 75.989998 */
- I>printf("%.2f\n", f); /* prints 75.99 */
- I>printf("%g\n", f); /* prints 75.99 */
- I>printf("%e\n", f); /* prints 7.599000e+01 */
- I>
- I>printf("%f\n", d); /* prints 75.990000 */
- I>printf("%g\n", d); /* prints 75.99 */
- I>printf("%e\n", d); /* prints 7.599 */
- I>}
- I>
- I>: I believe the correct answer involves using function floor but I can't
- I>: remember how.
- I>
- I>floor() will not help - it returns the largest integer not greater than
- I>its argument.
- I>
- I>Regards
- I>
- I>-A.
- I>--
- I>| A.Champion |
- I>
- I>
-
- Verne H. Bohlender
- Box 416, Chesley, Ontario.Ca. N0G 1L0
- verneb@The-Fix.sos.on.ca
- ___
- * UniQWK #2158*
-
-